//
//  WBStatuesViewModel.swift
//  XLWibo-Swift
//
//  Created by 王林 on 2016/11/16.
//  Copyright © 2016年 木木. All rights reserved.
//

import UIKit

class WBStatuesViewModel: NSObject {
    //头像图片
    var urlString :URL?
    //认证图片
    var  verifitedImage:UIImage?
    //会员等级
    var mRankImage:UIImage?
    
    //转发数
    var reposts_count:Int? = 0
    //评论数
    var comments_count:Int? = 0
    //点赞数
    var attitudes_count:Int? = 0

//    var repostsCount:Int? {
//        return viewmodel?.reposts_count == 0 ? 0 :viewmodel?.reposts_count
//    }
    //转换信息来源格式
    var soruceText:String?
    
    //为转发,和原创的类容获取图片
    var urlStringS:[WBPictureInfo]?{
        if viewmodel?.retweeted_status?.pic_urls == nil {
             return viewmodel?.pic_urls
        }
        return viewmodel?.retweeted_status?.pic_urls
    }
    //计算型属性
    var timeText:String?{
        let dateFormater = DateFormatter()
         //指定格式化符
         dateFormater.dateFormat   = "EEE MMM dd HH:mm:ss Z yyyy"
        //在真机上必须设置本地化信息,否则转化失败
        dateFormater.locale = Locale(identifier:"en")
        guard let sinaDate = dateFormater.date(from: viewmodel?.created_at  ?? " " ) else {
            return "外太空来的吧"
        }
        let calender = Calendar.current

        let currentDate = Date()
        let resule = calender.dateComponents([.day,.year], from: sinaDate,to: currentDate )

        if resule.year == 0 {
           //今年
            //获取当前时间相差的秒数
         let delta = currentDate.timeIntervalSince(sinaDate)
            if delta < 60 {
                return "刚刚"
            }else if delta < 3600{
                return"\(Int(delta/60))分钟前"
            }else{
                //其他 MM-得到HH:mm
                dateFormater.dateFormat = "MM-dd HH:mm"
                return dateFormater.string(from: sinaDate)
            }
        }else{
            //非当年
            dateFormater.dateFormat = "yyyy-MM-dd HH:mm"
            return dateFormater.string(from: sinaDate)
        }
        
    }
    
    
    
    var  viewmodel :WBStatuses?{
        didSet{
           urlString = URL(string: (viewmodel?.user?.avatar_large)!)
            
            //处理微博认证图片
            self.dealVerifitedImage()
            //处理等级图片
            self.dealMankImage()
            //处理信息来源
            self.soruceText =  dealWBSource(source:viewmodel?.source ?? " ")
        }
    }
    
    func dealWBSource(source:String)-> String? {
    let setaFlag = "\">"
        let endFlag = "</a>"
        guard let starRang  = source.range(of: setaFlag),
            let endRang = source.range(of: endFlag) else {
            return "我是👽"
        }
        /*
         upperBound 范围的最大边界
         lowerBound 范围的最小边界
         */
        let range = starRang.upperBound..<endRang.lowerBound
        let subStr = source.substring(with: range)
    
    return subStr
    }
    
    
    func dealVerifitedImage(){
        let type = viewmodel?.user?.verfied_type ?? -1
        
        switch type {
        case 0:
            verifitedImage = #imageLiteral(resourceName: "avatar_vip")
        case 0:
            verifitedImage = #imageLiteral(resourceName: "avatar_enterprise_vip")

        case 0:
            verifitedImage = #imageLiteral(resourceName: "avatar_grassroot")

        default:
            //没有认证
            verifitedImage = nil
        }
        
    }
    
    func dealMankImage(){
        let mbrank = viewmodel?.user?.mbrank ?? 0
        if mbrank > 0 && mbrank < 7 {
            //赋值
            mRankImage = UIImage(named:  "common_icon_membership_level\(mbrank)")
        }
    }
    
}










